package top.itmeans.yes.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.itmeans.yes.dto.OrderReturnApplyResult;
import top.itmeans.yes.dto.ReturnApplyQueryParam;
import top.itmeans.yes.dto.UpdateStatusParam;
import top.itmeans.yes.entity.OrderReturnApply;
import top.itmeans.yes.mapper.OrderReturnApplyMapper;
import top.itmeans.yes.service.OrderReturnApplyService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单退货申请 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OrderReturnApply> implements OrderReturnApplyService {

    @Resource
    private OrderReturnApplyMapper orderReturnApplyMapper;

    @Override
    public Page<OrderReturnApply> list(ReturnApplyQueryParam queryParam, Integer pageSize, Integer pageNum) {
        Page<OrderReturnApply> page = new Page<>(pageNum,pageSize);
        //page.setRecords(orderReturnApplyMapper.getList(queryParam));
        LambdaQueryWrapper<OrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StrUtil.isEmpty(queryParam.getHandleMan()), OrderReturnApply::getHandleMan,queryParam.getHandleMan())
                .eq(ObjectUtil.isNotNull(queryParam.getId()), OrderReturnApply::getId,queryParam.getId())
                .eq(ObjectUtil.isNotNull(queryParam.getStatus()), OrderReturnApply::getStatus,queryParam.getStatus())
                .like(!StrUtil.isEmpty(queryParam.getCreateTime()), OrderReturnApply::getCreateTime,queryParam.getCreateTime())
                .like(!StrUtil.isEmpty(queryParam.getHandleTime()), OrderReturnApply::getHandleTime,queryParam.getHandleTime())
                .like(!StrUtil.isEmpty(queryParam.getReceiverKeyword()), OrderReturnApply::getReturnName,queryParam.getReceiverKeyword())
                .or()
                .like(!StrUtil.isEmpty(queryParam.getReceiverKeyword()), OrderReturnApply::getReturnPhone,queryParam.getReceiverKeyword());
        return orderReturnApplyMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int delete(List<Long> ids) {
        return orderReturnApplyMapper.deleteBatchIds(ids);
    }

    @Override
    public OrderReturnApplyResult getItem(Long id) {
        return orderReturnApplyMapper.getDetail(id);
    }

    @Override
    public int updateStatus(Long id, UpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OrderReturnApply returnApply = new OrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(LocalDateTime.now());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        } else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(LocalDateTime.now());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        } else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(LocalDateTime.now());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        } else{
            return 0;
        }
        return orderReturnApplyMapper.updateById(returnApply);
    }
}
